package com.tone.ji_volunteer.controller;

import com.tone.ji_volunteer.pojo.Activity;
import com.tone.ji_volunteer.pojo.Joiner;
import com.tone.ji_volunteer.pojo.PageBean;
import com.tone.ji_volunteer.pojo.Result;
import com.tone.ji_volunteer.service.ActivityService;
import com.tone.ji_volunteer.service.LoginService;
import com.tone.ji_volunteer.util.JWT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * @author tone(ang)
 * version 1.0
 */
@RestController
@Slf4j
//@Scope("prototype")
@RequestMapping("/activities")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @GetMapping
    public Result list(@RequestParam(defaultValue = "1") Integer pagenum,
                       @RequestParam(defaultValue = "5") Integer pagesize,
                       @RequestParam() String query) {
        log.info("查询所有活动");
        PageBean list = activityService.list(pagenum, pagesize, query);
        return Result.success(list);
    }

    @GetMapping("/detail")
    public Result activityDetail(@RequestHeader("Authorization") String authHeader, Integer activity_id) {
        //System.out.println(activity_id);
        String openid = (String) JWT.parseJwt(authHeader).get("openid");
        Activity activity = activityService.selectActivity(openid, activity_id);
        return Result.success(activity);
    }

    @GetMapping("/search")
    public Result activitySearch(String query) {
        List<Activity> activities = activityService.activitySearch(query);
        return Result.success(activities);
    }

    @PostMapping("/add")
    public Result activityAdd(@RequestHeader("Authorization") String authHeader, @RequestBody Map<String, Object> body) {
        System.out.println("body=" + body);
        String openid = (String) JWT.parseJwt(authHeader).get("openid");
        Map<String, Object> activity = (Map<String, Object>) body.get("form");
        Integer id = activityService.addActivity(activity, openid);
        return Result.success(id);
    }

    @GetMapping("/images")
    public Result activityImages(Integer activity_id) {
        List<String> images = activityService.getImages(activity_id);
        return Result.success(images);
    }

    @GetMapping("/myfound")
    public Result myFoundActivity(@RequestHeader("Authorization") String authHeader) {
        String openid = (String) JWT.parseJwt(authHeader).get("openid");
        List<Activity> activities = activityService.myFoundActivity(openid);
        return Result.success(activities);
    }

    @PostMapping("/join")
    public Result joinActivity(@RequestHeader("Authorization") String authHeader, @RequestBody Map<String, Object> body) {
        String openid = (String) JWT.parseJwt(authHeader).get("openid");
        Integer activityId = (Integer) body.get("activityId");
        System.out.println(activityId);
        activityService.joinActivity(openid, activityId);
        return Result.success();
    }

    @PostMapping("/end")
    public Result endActivity(@RequestBody Map<String, Object> body) {
        Integer activityId = (Integer) body.get("activityId");
        System.out.println("activityId=" + activityId);
        activityService.endActivity(activityId);
        return Result.success();
    }

    @GetMapping("/isjoin")
    public Result isJoinActivity(@RequestHeader("Authorization") String authHeader, Integer activityId) {
        String openid = (String) JWT.parseJwt(authHeader).get("openid");
        Joiner join = activityService.isJoin(openid, activityId);
        return Result.success(join);
    }

    @GetMapping("/myparticipation")
    public Result myParticipation(@RequestHeader("Authorization") String authHeader) {
        String openid = (String) JWT.parseJwt(authHeader).get("openid");
        List<Activity> activities = activityService.myParticipation(openid);
        return Result.success(activities);
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteActivity(@PathVariable Integer id) throws Exception {
        activityService.deleteActivity(id);
        return Result.success();
    }
}
